<?php
/**
 * MetadataOption Dao definition
 *
 * @author fusq
 * @since 2009-03-25
 */

require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class MetadataOptionDao extends BaseDao{

    protected $_name;

    public function  _setup() {
        $this->_name = $this->prefix() . "metadata_options";
        parent::_setup();
    }

    public function insertMetadataOption($data){
       return $this->insertData($data);
    }

    public function getOptionsByMetadataId($metadataId){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where("metadata_id = ?", $metadataId);
        $select->where("is_delete = 0");
        //$select->order("title ASC");
        $sql = $select->__toString();
        $sql .= " ORDER BY CONVERT( title USING gbk )"; 
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }

    public function updateMetadataOption($data,$id){
        $where = $this->db_w->quoteInto('id = ?', $id);
        $this->updateData($data, $where);
    }

    public function updateByMetadataId($data,$metadataId){
        $where = $this->db_w->quoteInto('metadata_id = ?', $metadataId);
        $this->updateData($data, $where);
    }

    public function deleteOption($metadataId){
        $where = $this->db_w->quoteInto('metadata_id = ?', $metadataId);
        $this->delete($where);
    }
    
    public function deleteOptionsById($metadataIds){
    	$metadataIds = mysql_escape_string($metadataIds);
    	$where = "id in ($metadataIds)";
    	$this->delete($where);
    }

    public function getOptionByMetadataIdAndValue($metadataId,$value){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where("metadata_id = ?", $metadataId);
        $select->where("value = ?", $value);
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }
    
    public function getOptionById($id){
        $select = $this->db_r->select();
        $select->from($this->_name,"*");
        $select->where("id = ?", $id);
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchRow($sql);
        return $result;
    }    
}